package com.etime.shycourse.services.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.etime.shycourse.dao.LessonMapper;
import com.etime.shycourse.dao.MediaNotifyDao;
import com.etime.shycourse.dao.NotifyDao;
import com.etime.shycourse.pojo.Lesson;
import com.etime.shycourse.pojo.MediaNotify;
import com.etime.shycourse.pojo.Notify;
import com.etime.shycourse.services.interfaces.MediaNotifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 云点播回调业务逻辑层实现类
 */
@Service
public class MediaNotifyServiceImpl implements MediaNotifyService {

    @Autowired
    private NotifyDao notifyDao;
    @Autowired
    private MediaNotifyDao mediaNotifyDao;
    @Autowired
    private LessonMapper lessonMapper;
    @Override
    public String medianotify(JSONObject object) {
        //1 保存原始回调消息
        int rtn1 = insertnotify(object);
        //2 拆分回调消息
        String taskid = "";
        String status = "";
        String fileid = "";
        String fileurl = "";
        String outputurl = "";
        Date outputtime = new Date();
        String eventtype = "";
        Integer Duration = 0;
        eventtype = object.get("EventType").toString();
        JSONObject ProcedureStateChangeEvent = object.getJSONObject("ProcedureStateChangeEvent");
        fileid = ProcedureStateChangeEvent.get("FileId").toString();
        status = ProcedureStateChangeEvent.get("Status").toString();
        taskid = ProcedureStateChangeEvent.get("TaskId").toString();
        fileurl = ProcedureStateChangeEvent.get("FileUrl").toString();
        JSONArray MediaProcessResultSet = ProcedureStateChangeEvent.getJSONArray("MediaProcessResultSet");
        for(int i=0;i<MediaProcessResultSet.size();i++){
            JSONObject obj = MediaProcessResultSet.getJSONObject(i);
            if(obj.get("Type").equals("Transcode")){
                outputurl = obj.getJSONObject("TranscodeTask").getJSONObject("Output").get("Url").toString();
                Duration = (int)Double.parseDouble(obj.getJSONObject("TranscodeTask").getJSONObject("Output").get("Duration").toString());
                break;
            }
        }
        MediaNotify notify = new MediaNotify();
        notify.setFileid(fileid);
        notify.setEventtype(eventtype);
        notify.setFileurl(fileurl);
        notify.setTaskid(taskid);
        notify.setOutputurl(outputurl);
        notify.setOutputtime(outputtime);
        notify.setStatus(status);
        //2 保存回调明细
        int rtn2 = mediaNotifyDao.insert(notify);
        //3 保存新的路径到课时中
        Lesson lesson = new Lesson();
        lesson.setLessonmedianame(fileid);
        lesson.setLessonmediaurl(outputurl);
        lesson.setLessonlen(Duration);
        UpdateWrapper<Lesson> wrapper = new UpdateWrapper<>();
        wrapper.eq("lessonmedianame",fileid);
        int rtn3 = lessonMapper.update(lesson,wrapper);
        //如果保存失败，则在notify中记录一条没有保存好的记录
        if(rtn3 == 0) {
            Notify notify2 = new Notify();
            notify2.setPost(fileid);
            notify2.setCreatedtime(new Date());
            notifyDao.insert(notify2);
        }
        return "SECCUES";
    }

    /**
     * 保存原始回调消息
     * @param object
     * @return
     */
    private int insertnotify(JSONObject object){
        Notify notify = new Notify();
        notify.setPost(object.toJSONString());
        notify.setCreatedtime(new Date());
        return notifyDao.insert(notify);
    }
}
